package com.shaoshaohuo.app.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.shaoshaohuo.app.entity.PayResultEneity;
import com.shaoshaohuo.app.framework.Logger;
import com.shaoshaohuo.app.framework.Logger.LogTag;
import com.shaoshaohuo.app.ui.FinishPayMoney;
import com.shaoshaohuo.app.ui.pay.PayResultActivity;
import com.shaoshaohuo.app.ui.pay.RechargeResultActivity;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Date;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    /**
     * 当前正在进行的微信支付对象
     */
    private static PayResultEneity currentPayEntity;
    /**
     * 充值
     */
    public static final String PAY_RECHAEGE = "PAY_RECHAEGE";
    /**
     * 电商
     */
    public static final String PAY_ORDER_EC = "PAY_ORDER_EC";
    /**
     * 物流
     */
    public static final String PAY_ORDER_SHIPPER = "PAY_ORDER_SHIPPER";
    /**
     * 当前的支付方式
     */
    private static String currentPayWay;

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(final BaseResp resp) {


        Logger.d(LogTag.UI, resp.errStr + "-" + resp.errCode + "-" + resp.openId + "-" + resp.transaction);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                Intent intent = new Intent(getApplicationContext(), FinishPayMoney.class);
                Date date = new Date();
                intent.putExtra("time", date.getTime());
                intent.putExtra("orderid", Aoride);
                startActivity(intent);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        ToastUtil.toast("支付过" + resp.openId);
                        Log.e(TAG, "run: " + resp.errStr + "-" + resp.errCode + "-" + resp.openId + "-" + resp.transaction);
                        Intent intent1 = new Intent();
                        intent1.setAction("finsh");
                        sendBroadcast(intent1);

                        ToastUtil.toast("支付成功");
                        finish();
                    }
                });
//                if (PAY_RECHAEGE.equals(getCurrentPayWay())) {
//                    Intent successIntent = new Intent(WXPayEntryActivity.this, RechargeResultActivity.class);
//                    successIntent.putExtra(PayResultActivity.KEY_RESULT, RechargeResultActivity.VALUE_SUCCESS);
//                    successIntent.putExtra(PayResultActivity.KEY_PAY_WAY, "微信");
//                    successIntent.putExtra(PayResultActivity.FORWARD_KEY, RechargeResultActivity.FORWARD_USER_MAIN);
//                    successIntent.putExtra(PayResultActivity.KEY_MONEY, getCurrentPayEntity().getData().getMoney() + "元");
//                    successIntent.putExtra(PayResultActivity.KEY_ORDER_ID, getCurrentPayEntity().getData().getOrderid());
//                    successIntent.putExtra(PayResultActivity.KEY_PAY_TIME, getCurrentPayEntity().getData().getPaytime());
//                    startActivity(successIntent);
//                } else if (PAY_ORDER_EC.equals(getCurrentPayWay())) {
//                    Intent successIntent = new Intent(WXPayEntryActivity.this, PayResultActivity.class);
//                    successIntent.putExtra(PayResultActivity.KEY_RESULT, PayResultActivity.VALUE_SUCCESS);
//                    successIntent.putExtra(PayResultActivity.KEY_PAY_WAY, "微信");
//                    successIntent.putExtra(PayResultActivity.FORWARD_KEY, PayResultActivity.FORWARD_EC_ORDER_LIST);
//                    successIntent.putExtra(PayResultActivity.KEY_MONEY, getCurrentPayEntity().getData().getMoney() + "元");
//                    successIntent.putExtra(PayResultActivity.KEY_ORDER_ID, getCurrentPayEntity().getData().getOrderid());
//                    successIntent.putExtra(PayResultActivity.KEY_PAY_TIME, getCurrentPayEntity().getData().getPaytime());
//                    startActivity(successIntent);
//                } else if (PAY_ORDER_SHIPPER.equals(getCurrentPayWay())) {
//                    Intent successIntent = new Intent(WXPayEntryActivity.this, PayResultActivity.class);
//                    successIntent.putExtra(PayResultActivity.KEY_RESULT, PayResultActivity.VALUE_SUCCESS);
//                    successIntent.putExtra(PayResultActivity.KEY_PAY_WAY, "微信");
//                    successIntent.putExtra(PayResultActivity.FORWARD_KEY, PayResultActivity.FORWARD_CART_ORDER_LIST);
//                    successIntent.putExtra(PayResultActivity.KEY_MONEY, getCurrentPayEntity().getData().getMoney() + "元");
//                    successIntent.putExtra(PayResultActivity.KEY_ORDER_ID, getCurrentPayEntity().getData().getOrderid());
//                    successIntent.putExtra(PayResultActivity.KEY_PAY_TIME, getCurrentPayEntity().getData().getPaytime());
//                    startActivity(successIntent);
//                }
//                setCurrentPayEntity(null);
//                setCurrentPayWay(null);
//                finish();

                return;
            } else if (resp.errCode == -1) {
                String reason = "支付错误";
                if (PAY_RECHAEGE.equals(getCurrentPayWay())) {
                    Intent successIntent = new Intent(WXPayEntryActivity.this, RechargeResultActivity.class);
                    successIntent.putExtra(PayResultActivity.FORWARD_KEY, PayResultActivity.FORWARD_USER_MAIN);
                    successIntent.putExtra(PayResultActivity.KEY_RESULT, PayResultActivity.VALUE_FAILURE);
                    successIntent.putExtra(PayResultActivity.KEY_FAILURE_REASON, reason);
                    startActivity(successIntent);
                }
                finish();
                return;
            } else if (resp.errCode == -2) {
                String reason = "支付取消";
                if (PAY_RECHAEGE.equals(getCurrentPayWay())) {
                    Intent successIntent = new Intent(WXPayEntryActivity.this, RechargeResultActivity.class);
                    successIntent.putExtra(PayResultActivity.FORWARD_KEY, PayResultActivity.FORWARD_USER_MAIN);
                    successIntent.putExtra(PayResultActivity.KEY_RESULT, PayResultActivity.VALUE_FAILURE);
                    successIntent.putExtra(PayResultActivity.KEY_FAILURE_REASON, reason);
                    startActivity(successIntent);
                }
                finish();
                return;
            }
        } else {
            finish();
        }

    }

    public static PayResultEneity getCurrentPayEntity() {
        return currentPayEntity;
    }

    public static void setCurrentPayEntity(PayResultEneity currentPayEntity) {
        WXPayEntryActivity.currentPayEntity = currentPayEntity;
    }

    public static String getCurrentPayWay() {
        return currentPayWay;
    }

    public static void setCurrentPayWay(String currentPayWay) {
        WXPayEntryActivity.currentPayWay = currentPayWay;
    }

    private static String Aoride = "";

    public static void setoride(String oride) {
        Aoride = oride;
    }

}